package behavioralPatterns.interpreter;

interface Node {
    int interpret();
}

class ValueNode implements Node {
    private int value;

    public ValueNode(int value) {
        this.value = value;
    }

    public int interpret() {
        return value;
    }
}

abstract class SymbolNode implements Node {
    protected Node left;
    protected Node right;

    public SymbolNode(Node left, Node right) {
        this.left = left;
        this.right = right;
    }

    public abstract int interpret();
}

class AddNode extends SymbolNode {
    public AddNode(Node left, Node right) {
        super(left, right);
    }

    public int interpret() {
        return left.interpret() + right.interpret();
    }
}

class SubtractNode extends SymbolNode {
    public SubtractNode(Node left, Node right) {
        super(left, right);
    }

    public int interpret() {
        return left.interpret() - right.interpret();
    }
}

class MultiplyNode extends SymbolNode {
    public MultiplyNode(Node left, Node right) {
        super(left, right);
    }

    public int interpret() {
        return left.interpret() * right.interpret();
    }
}

class DivideNode extends SymbolNode {
    public DivideNode(Node left, Node right) {
        super(left, right);
    }

    public int interpret() {
        return left.interpret() / right.interpret();
    }
}

class Interpreter {
    private String statement;

    public void build(String statement) {
        this.statement = statement;
    }

    public int interpret() {
        Node root = buildTree(statement);
        return root.interpret();
    }

    private Node buildTree(String statement) {
        String[] tokens = statement.split("");
        Node left = null;
        Node right = null;
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("+")) {
                left = buildTree(statement.substring(0, i));
                right = buildTree(statement.substring(i + 1));
                return new AddNode(left, right);
            }
            if (tokens[i].equals("-")) {
                left = buildTree(statement.substring(0, i));
                right = buildTree(statement.substring(i + 1));
                return new SubtractNode(left, right);
            }
            if (tokens[i].equals("*")) {
                left = buildTree(statement.substring(0, i));
                right = buildTree(statement.substring(i + 1));
                return new MultiplyNode(left, right);
            }
            if (tokens[i].equals("/")) {
                left = buildTree(statement.substring(0, i));
                right = buildTree(statement.substring(i + 1));
                return new DivideNode(left, right);
            }
        }
        return new ValueNode(Integer.parseInt(statement));
    }
}

public class InterpreterPattern {
    public static void main(String[] args) {
        Interpreter interpreter = new Interpreter();
        interpreter.build("5+3*2-1");
        int result = interpreter.interpret();
        System.out.println(result); // Output: 8 未实现先乘除后加减，待完善
    }
}
